﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdateUOMHandler : ICommandHandler<CreateOrUpdateUOMCommand>
    {
        private readonly IUOMRepository uomRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateUOMHandler(IUOMRepository uomRepository, IUnitOfWork unitOfWork)
        {
            this.uomRepository = uomRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateUOMCommand command)
        {
            var uom = new UOM
            {
                ID = command.ID,
                Active=command.Active,
                Code=command.Code,
                Description=command.Description,
                Name=command.Name,
                Quantity=command.Quantity,
                Ratio=command.Ratio,
                RoundPrecision=command.RoundPrecision,
                Type=command.Type,
            };
            if (uom.ID == 0)
                uomRepository.Add(uom);
            else
                uomRepository.Update(uom);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateUOMCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateUOMCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
